package sg.edu.nus.iss.cats.controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.dataaccess.DAOException;

public class ShowUsersServlet extends HttpServlet {

   private static final String SHOWALL="all";

   public void doGet (HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
   
            String pathInfo = request.getPathInfo();
            
            System.out.println("ShowUsersServlet: " + pathInfo);
            response.setContentType ("text/html");
            
            if (pathInfo == null || pathInfo.equals(SHOWALL)) {
                showAllUsers(request,response);
            } else {
            
                showUser(request, response,pathInfo);
            }
            
            
    }
    
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
    
        doGet(request, response);
    }
    
    
    //Get all users details and display
    private void showAllUsers(HttpServletRequest request,HttpServletResponse response) throws IOException{
    
        System.out.println("ShowUsersServlet: in showAllUsers");
        //PrintWriter out = response.getWriter ();
        
        
        User[] users = null;
        try {
            //Get all users details from DB and display
            UserManager userManager = new UserManager();
            users = userManager.findAllUsers();
            
            System.out.println("ShowUsersServlet: got users" + users.length);
        }catch (DAOException e){
        
            HTMLUtil.displayException(request,response,"Sorry, an error occurred getting the users details");
            return;
        }  
        
        displayUsers(request,response,users);
        
    }
    
    //Get a particular user's details and display
    private void showUser(HttpServletRequest request,HttpServletResponse response,String pathInfo) throws IOException{
    
         System.out.println("ShowUsersServlet: in showUser" + pathInfo);
         //PrintWriter out = response.getWriter ();   
         
         String userId = HTMLUtil.stripUserId(pathInfo);
         
        //Get user details from DB and display
         User[] users = new User[1];
     
         try {
            UserManager userManager = new UserManager();
            users[0] = userManager.findUser(userId);
           
            displayUsers(request,response,users);
            
         }catch(DAOException e){
         
            HTMLUtil.displayException(request,response,"Sorry, information on user: " + userId + " is not available");
            return;
         }
    }
    
    
    //Display users' details
    private void displayUsers(HttpServletRequest request,HttpServletResponse response,User[] users) throws IOException{
    
        HTMLUtil.displayUsers(request,response,users);
        
    }
    
   

}